<?php
class Index extends Controller {
	public function html() {
		$mUser = new User_model();
		$currUser = $mUser->currUser();
		if(!$currUser) header('Location:/auth/login?backurl='.urlencode('/sales/'));
		
		$data = array();
		$tpl = 'sales/index.html';
		
		$oid = $this->get('oid', 'str');
		$nickname = $this->get('nickname', 'str');
		$status = $this->get('status', 'int');
		$page = $this->get('page', 'str', 1);
		$size = 20;
		
		$params = array();
		if($oid) $params['oid'] = $oid;
		if($nickname) {
			$mMember = new Member_model();
			$userInfo = $mMember->userInfoByNickname($nickname);
			if($userInfo) {
				$params['uid'] = $userInfo['uid'];
			}
		}
		if($status) $params['status'] = $status-1;
		$mOrder = new Order_model();
		$data['order'] = $mOrder->page($page, $size, $params);
		
		if($data['order']) {
			$uid = array();
			$product = array();
			$ticket = array();
			foreach ($data['order']['data'] as $order) {
				$uid[] = $order['uid'];
				$product[] = $order['product_id'];
				$ticket[] = $order['ticket_id'];
			}
		
			//获取用户名
			$mMember = new Member_model();
			$user = $mMember->userInfoByUid($uid);
			
			//获取景点
			$mProduct = new Product_model();
			$product = $mProduct->productInfoByid($product);
			
			//获取门票
			$mTicket = new Ticket_model();
			$ticket = $mTicket->ticketInfoByTid($ticket);
			
			foreach ($data['order']['data'] as $key => $order) {
				$data['order']['data'][$key]['user'] = $user[$order['uid']];
				$data['order']['data'][$key]['product'] = $product[$order['product_id']];
				$data['order']['data'][$key]['ticket'] = $ticket[$order['ticket_id']];
			}
		}
		
		$data['oid'] = $oid;
		$data['nickname'] = $nickname;
		$data['status'] = $status;
		
		$this->display($data, $tpl);
	}
}